const jwt = require("jsonwebtoken")
const secretAdmin = 'xiaoyuankuaidiAdmin'
const secretUser = 'xiaoyuankuaidiUser'
module.exports = {

    checkTokenAdmin(req, res, next) {
        const token = req.headers['token']
        if (!token)
            return res.status(401).json({ message: '请传token' })
        jwt.verify(token, secretAdmin, (err, decoded) => {
            if (err) return res.status(500).json({ message: "token不正确" })
            req.userId = decoded.id
            req.name = decoded.name
            next()
            // return res.status(401).json({ message: '权限不足' })

        })
    },
    getTokenAdmin(id, name) {
        if (!id && name) {
            return new Error("请传user信息")
        }
        let payload = {
            name: name,
            id: id,
        }
        return jwt.sign(payload, secretAdmin, { expiresIn: '24h' })
    },
    getTokenUser(id, phone) {
        if (!id && name) {
            return new Error("请传user信息")
        }
        let payload = {
            id: id,
            phone: phone
        }
        return jwt.sign(payload, secretUser, { expiresIn: '1080h' })
    },
    checkTokenUser(req, res, next) {
        const token = req.headers['token']
        if (!token)
            return res.status(401).json({ message: '请传token' })
        jwt.verify(token, secretUser, (err, decoded) => {
            if (err) return res.status(500).json({ message: "token不正确" })
            req.userId = decoded.id
            req.phone = decoded.phone
            next()
            // return res.status(401).json({ message: '权限不足' })

        })
    }
}